
'use strict';
// const revcollector = require('gulp-rev-collector'); // 修改依赖资源路径
// const uglify = require('gulp-uglify'); // js压缩
// const jsconcat = require(''); // js丑化 gulp-concat
// const htmlmin = require('gulp-htmlmin'); // 压缩html
// const less = require('gulp-less'); // less编译
const gulp = require('gulp');
const colors = require('colors');
const gulpHtml = require('gulp-html-tpl');
const artTemplate = require('art-template');
const minifyCss = require('gulp-minify-css'); // css压缩
const autoprefixer = require('gulp-autoprefixer'); // 自动补全前缀
const sass = require('gulp-sass');
const imagemin = require('gulp-imagemin'); // 图片压缩
const imageminJpegRecompress = require('imagemin-jpeg-recompress');
const imageminOptipng = require('imagemin-optipng');
const browserSync = require('browser-sync').create(); // 浏览器自动刷新
const del = require('del');
const runSequence = require('run-sequence');
const port = 8899;

const filePath = require('./build/config.js');

gulp.task('htmlTpl', function () {
  gulp.src(filePath.devhtml)
    .pipe(gulpHtml({
      tag: 'template',
      paths: ['src/components/'],
      engine: function (template, data) {
        return artTemplate.compile(template)(data)
      },
      data: {
        useHeader: false
      },
      beautify: {
        indent_char: ' ',
        indent_with_tabs: false
      }
    }))
    .pipe(gulp.dest(filePath.disthtml));
});
gulp.task('sass', function () {
  gulp.src(filePath.devcss)
    // 嵌套输出方式 nested
    // 展开输出方式 expanded 
    // 紧凑输出方式 compact 
    // 压缩输出方式 compressed
    .pipe(sass({ outputStyle: 'expanded' }).on('error', sass.logError))
    .pipe(autoprefixer('last 10 version'))
    .pipe(gulp.dest(filePath.distcss));
});
gulp.task('jsmin', function () {
  gulp.src(filePath.devjs)
    // .pipe(uglify()) // 代码压缩
    .pipe(gulp.dest(filePath.distjs));
});
//图片压缩任务，支持JPEG、PNG、及GIF文件;
gulp.task('imgmin', function () {
  const jpgmin = imageminJpegRecompress({
    accurate: true,//高精度模式
    quality: "low",//图像质量:low,medium,high and veryhigh
    method: "smallfry",//网络优化:mpe,ssim,ms-min and smallfry
    min: 40,//最低质量
    loops: 2,//循环尝试次数，默认为6
    progressive: false,//基线优化
    subsmaple: "default"//子采样:default,disable
  })
  const pngmin = imageminOptipng({
    optimizationLevel: 4, // 1 - 7
    bitDepthReduction: true
  });
  gulp.src(filePath.devimg)
    .pipe(imagemin(
      //   {
      //   use: [jpgmin, pngmin] // 压缩效果不明显不知缘由，配置参数无效
      // }
      [
        imagemin.gifsicle({ interlaced: true }),
        imagemin.jpegtran({ progressive: true, uality: "low", min: 50}),
        imagemin.optipng({ optimizationLevel: 5 }),
        imagemin.svgo({
          plugins: [
            { removeViewBox: true },
            { cleanupIDs: false }
          ]
        })
      ]
    ))
    .pipe(gulp.dest(filePath.distimg))
});

//lib迁移
gulp.task('copyLib', function () {
  gulp.src(filePath.devlib)
    .pipe(gulp.dest(filePath.distlib));
});

//服务器任务，以根目录为入口 服务启动慢，关闭网络请求
gulp.task('server', function () {
  browserSync.init({
    server: {
      baseDir: "./",
      directory: true,
    },
    port: port,
    online: true
  })
});
gulp.task('watch', function () {
  // 第一个数组参数是要监听的目录，第二个数组参数是监听之后要执行的任务
  gulp.watch(['./src/css/**/*.*', './src/css/*.*'], ['sass']).on('change', browserSync.reload)
  gulp.watch(['./src/js/**/*.*', './src/js/*.*'], ['jsmin']).on('change', browserSync.reload)
  gulp.watch(['./src/img/**/*.*', './src/img/*.*'], ['imgmin']).on('change', browserSync.reload)
  gulp.watch(['./src/lib/**/*.*', './src/lib/*.*'], ['copyLib']).on('change', browserSync.reload)
  gulp.watch([
    './src/**/*.html',
    './src/*.html',
  ], ['htmlTpl']).on('change', browserSync.reload)
});
gulp.task('clear', function (cb) {
  del([
    './dist',
    // 我们不希望删掉这个文件，所以我们取反这个匹配模式
    // '!dist/mobile/deploy.json'
  ]).then(() => {
    console.log(colors.green('dist目录已清理'));
    runSequence(['server', 'htmlTpl', 'imgmin', 'sass', 'jsmin', 'copyLib'], 'watch')
  }).catch(() => {
    console.log(colors.red('文件清理失败'));
  });
})
gulp.task('default', runSequence('clear'))


gulp.task('imfyimg', function() {
  console.log(0)
})